{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3a0ca5b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_KEY = 'DPYYDCkHffqH7U7CecHqDbF3MdgiylB5'\n",
    "API_S = 'J5GsoExT0HlODW1yfj93NHxa4IhhHS1j'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fd19a29d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "48f146b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    d:\\api\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a41e3d3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477955,5f99975b-5185-419a-9c69-6da02aff81e0',\n",
       " 'time_used': 599,\n",
       " 'faces': [{'face_token': '6d12b82598e9f69640297846dfa1aea8',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': '3XXAy6wuwDnc0I+ASLvc3Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "746be566",
   "metadata": {},
   "source": [
    "# analyse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c31c023b",
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_KEY,API_S,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3860931c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2686af4f127826dd2ca26b467563dadc'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens = face1['faces'][0]['face_token']\n",
    "face_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "990fdf23",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a0841354",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_sercret, face_tokens, landmark=1, attributes=None, face_rectangle=None, beauty_score_min=0, beauty_score_max=100)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    d:\\api\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "504abb30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 82,\n",
       " 'request_id': '1648477956,55cb6be7-3543-4681-8871-44a937e45cd7',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 174, 'x': 199},\n",
       "    'mouth_upper_lip_top': {'y': 171, 'x': 214},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 171, 'x': 208},\n",
       "    'left_eye_upper_left_quarter': {'y': 123, 'x': 179},\n",
       "    'left_eyebrow_lower_middle': {'y': 112, 'x': 178},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 175, 'x': 202},\n",
       "    'right_eye_top': {'y': 115, 'x': 237},\n",
       "    'left_eye_bottom': {'y': 127, 'x': 186},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 105, 'x': 231},\n",
       "    'right_eye_pupil': {'y': 118, 'x': 236},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 180, 'x': 230},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 186, 'x': 228},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 180, 'x': 237},\n",
       "    'contour_chin': {'y': 218, 'x': 219},\n",
       "    'contour_left9': {'y': 215, 'x': 205},\n",
       "    'left_eye_lower_right_quarter': {'y': 126, 'x': 191},\n",
       "    'mouth_lower_lip_top': {'y': 184, 'x': 215},\n",
       "    'right_eyebrow_upper_middle': {'y': 96, 'x': 242},\n",
       "    'left_eyebrow_left_corner': {'y': 121, 'x': 167},\n",
       "    'right_eye_bottom': {'y': 120, 'x': 237},\n",
       "    'contour_left7': {'y': 199, 'x': 185},\n",
       "    'contour_left6': {'y': 189, 'x': 176},\n",
       "    'contour_left5': {'y': 178, 'x': 170},\n",
       "    'contour_left4': {'y': 167, 'x': 167},\n",
       "    'contour_left3': {'y': 154, 'x': 165},\n",
       "    'contour_left2': {'y': 142, 'x': 164},\n",
       "    'contour_left1': {'y': 130, 'x': 164},\n",
       "    'left_eye_lower_left_quarter': {'y': 128, 'x': 180},\n",
       "    'contour_right1': {'y': 118, 'x': 277},\n",
       "    'contour_right3': {'y': 146, 'x': 280},\n",
       "    'contour_right2': {'y': 132, 'x': 279},\n",
       "    'mouth_left_corner': {'y': 179, 'x': 192},\n",
       "    'contour_right4': {'y': 160, 'x': 280},\n",
       "    'contour_right7': {'y': 197, 'x': 260},\n",
       "    'right_eyebrow_left_corner': {'y': 105, 'x': 220},\n",
       "    'nose_right': {'y': 151, 'x': 230},\n",
       "    'nose_tip': {'y': 152, 'x': 208},\n",
       "    'contour_right5': {'y': 174, 'x': 277},\n",
       "    'nose_contour_lower_middle': {'y': 162, 'x': 211},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 116, 'x': 172},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 189, 'x': 205},\n",
       "    'right_eye_right_corner': {'y': 118, 'x': 247},\n",
       "    'right_eye_lower_right_quarter': {'y': 120, 'x': 243},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 169, 'x': 232},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 106, 'x': 251},\n",
       "    'left_eye_left_corner': {'y': 127, 'x': 175},\n",
       "    'mouth_right_corner': {'y': 172, 'x': 243},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 172, 'x': 229},\n",
       "    'right_eye_lower_left_quarter': {'y': 121, 'x': 232},\n",
       "    'left_eyebrow_right_corner': {'y': 109, 'x': 195},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 111, 'x': 187},\n",
       "    'right_eye_center': {'y': 119, 'x': 237},\n",
       "    'nose_left': {'y': 155, 'x': 193},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 183, 'x': 202},\n",
       "    'left_eye_upper_right_quarter': {'y': 122, 'x': 191},\n",
       "    'right_eyebrow_lower_middle': {'y': 104, 'x': 241},\n",
       "    'left_eye_top': {'y': 121, 'x': 185},\n",
       "    'left_eye_center': {'y': 125, 'x': 186},\n",
       "    'contour_left8': {'y': 207, 'x': 194},\n",
       "    'contour_right9': {'y': 214, 'x': 235},\n",
       "    'right_eye_left_corner': {'y': 121, 'x': 227},\n",
       "    'mouth_lower_lip_bottom': {'y': 189, 'x': 216},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 111, 'x': 169},\n",
       "    'left_eye_pupil': {'y': 124, 'x': 185},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 97, 'x': 229},\n",
       "    'contour_right8': {'y': 206, 'x': 248},\n",
       "    'right_eyebrow_right_corner': {'y': 110, 'x': 260},\n",
       "    'right_eye_upper_left_quarter': {'y': 117, 'x': 231},\n",
       "    'left_eyebrow_upper_middle': {'y': 105, 'x': 176},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 100, 'x': 253},\n",
       "    'nose_contour_left1': {'y': 126, 'x': 202},\n",
       "    'nose_contour_left2': {'y': 145, 'x': 198},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 169, 'x': 219},\n",
       "    'nose_contour_right1': {'y': 124, 'x': 219},\n",
       "    'nose_contour_right2': {'y': 143, 'x': 225},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 185, 'x': 198},\n",
       "    'contour_right6': {'y': 186, 'x': 270},\n",
       "    'nose_contour_right3': {'y': 158, 'x': 221},\n",
       "    'nose_contour_left3': {'y': 160, 'x': 201},\n",
       "    'left_eye_right_corner': {'y': 126, 'x': 197},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 104, 'x': 186},\n",
       "    'right_eye_upper_right_quarter': {'y': 116, 'x': 242},\n",
       "    'mouth_upper_lip_bottom': {'y': 174, 'x': 214}},\n",
       "   'face_rectangle': {'width': 118, 'top': 98, 'left': 164, 'height': 118},\n",
       "   'face_token': '2686af4f127826dd2ca26b467563dadc'}]}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_KEY,API_S,face_tokens)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2304e9ed",
   "metadata": {},
   "source": [
    "# compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7c55b1b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477957,76ac6e21-4fb1-496a-b2e9-bbb432ee989a',\n",
       " 'time_used': 269,\n",
       " 'faces': [{'face_token': 'b0c35b71ce8d9ebf706bac18ca9b5a0a',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': '3XXAy6wuwDnc0I+ASLvc3Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "51a1b008",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2686af4f127826dd2ca26b467563dadc'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token1 = face1['faces'][0]['face_token']\n",
    "face_token1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8c83064c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477957,a21b0642-aaf3-424f-aa02-dfd91ce092e6',\n",
       " 'time_used': 43,\n",
       " 'faces': [{'face_token': '2abdb4e4bcb98cd3587967f81b44eb90',\n",
       "   'face_rectangle': {'top': 62, 'left': 140, 'width': 81, 'height': 81}}],\n",
       " 'image_id': 'uwuFcFPPfmsroBMHTKhxUA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'Liu_02.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "211793b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "face2 = detect.face_detect(API_KEY,API_S,'Liu_02.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5c93c166",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fd2580293c2d9d4b46e535ca029b6df7'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token2 = face2['faces'][0]['face_token']\n",
    "face_token2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c961e4ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "094b793c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_sercret, face_token1, face_token2)\n",
      "        该函数为调用face++ face_compare接口\n",
      "\n",
      "FILE\n",
      "    d:\\api\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fbfd4b13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477958,4453ac75-6100-434a-a390-5f29130dbfab',\n",
       " 'time_used': 282,\n",
       " 'confidence': 91.769,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_KEY,API_S,face_token1,face_token2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2b7a209",
   "metadata": {},
   "source": [
    "# search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "dccd11de",
   "metadata": {},
   "outputs": [],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ec01161f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 93,\n",
       " 'facesets': [{'faceset_token': 'f74252466d2c71ef341b18806ae8d36b',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '6779ae69fe2c2f8b3b12e217601243ed',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '05ce4188949953251808494d802ea884',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'c37c1ddf34c54718d310dc896da667c6',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '5cd7596ca27561645001447d0984ccab',\n",
       "   'outer_id': 'API020305',\n",
       "   'display_name': '2020级网新API周二上午班',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648477958,6ad0b4f3-75c5-4728-b9a9-36623b8ac767'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fad5aa92",
   "metadata": {},
   "outputs": [],
   "source": [
    "faceset_tokens = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "26c6e8a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f74252466d2c71ef341b18806ae8d36b'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = faceset_tokens['facesets'][0]['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c48c5497",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477998,2caaf388-dd2d-4e70-b94b-a75395863063',\n",
       " 'time_used': 37,\n",
       " 'faces': [{'face_token': 'af8166bcade78f80bac48a1fdfe5cb07',\n",
       "   'face_rectangle': {'top': 65, 'left': 117, 'width': 75, 'height': 75}}],\n",
       " 'image_id': 'xBRj0gSzSDYK3VBw+9WM+A==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'Liu_03.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "89b8b8de",
   "metadata": {},
   "outputs": [],
   "source": [
    "face3 = detect.face_detect(API_KEY,API_S,'Liu_03.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "560ed74a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b3c4eb639fbe966a9d4ad205c3c7d323'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = face3['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5c6f5431",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bb99b1e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module search:\n",
      "\n",
      "NAME\n",
      "    search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_key, API_sercret, faceset_token, face_token)\n",
      "        该函数为调用face++ face_search接口\n",
      "\n",
      "FILE\n",
      "    d:\\api\\search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2b36c7ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 84,\n",
       " 'error_message': 'EMPTY_FACESET',\n",
       " 'request_id': '1648478003,1ae6261c-c068-4c80-8e56-c4666df6453f'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_KEY,API_S,faceset_token,face_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10e43a62",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
